from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone
from django.core.validators import MinValueValidator, MaxValueValidator
from .student import Student  # 假设Student模型位于core.models.student
from .base_model import BaseModel


class HonorType(BaseModel):
    """表彰类型，如奖学金、三好学生、优秀干部等"""
    name = models.CharField('表彰类型', max_length=50, unique=True)
    description = models.TextField('类型描述', blank=True, null=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '表彰类型'
        verbose_name_plural = '表彰类型管理'


class HonorRecord(BaseModel):
    """表彰记录"""
    student = models.ForeignKey(Student, on_delete=models.CASCADE, verbose_name='学生')
    honor_type = models.ForeignKey(HonorType, on_delete=models.PROTECT, verbose_name='表彰类型')
    title = models.CharField('表彰标题', max_length=100)
    award_date = models.DateField('颁奖日期')
    award_organizer = models.CharField('颁奖组织', max_length=100)
    description = models.TextField('表彰详情', blank=True, null=True)
    reward = models.CharField('奖励内容', max_length=200, blank=True, null=True)
    certificate_number = models.CharField('证书编号', max_length=50, blank=True, null=True)
    points_added = models.IntegerField('加分值', validators=[MinValueValidator(0), MaxValueValidator(100)], blank=True, null=True)
    recorder = models.ForeignKey(User, on_delete=models.SET_NULL, blank=True, null=True, verbose_name='记录人')

    def __str__(self):
        return f"{self.student.name} - {self.title} - {self.award_date.strftime('%Y-%m-%d')}"

    class Meta:
        verbose_name = '表彰记录'
        verbose_name_plural = '表彰记录管理'
        ordering = ['-award_date']


class HonorCertificate(BaseModel):
    created_at = models.DateTimeField(default=timezone.now, verbose_name="创建时间")
    """表彰证书"""
    record = models.OneToOneField(HonorRecord, on_delete=models.CASCADE, verbose_name='表彰记录')
    certificate_file = models.FileField('证书文件', upload_to='honor_certificates/%Y/%m/%d/')
    is_verified = models.BooleanField('已验证', default=False)
    verified_by = models.ForeignKey(User, on_delete=models.SET_NULL, blank=True, null=True, related_name='verified_certificates', verbose_name='验证人')
    verified_at = models.DateTimeField('验证时间', blank=True, null=True)

    def verify(self, user):
        self.is_verified = True
        self.verified_by = user
        self.verified_at = timezone.now()
        self.save()

    def __str__(self):
        return f"{self.record.student.name} - {self.record.title} 证书"

    class Meta:
        verbose_name = '表彰证书'
        verbose_name_plural = '表彰证书管理'


class SemesterHonor(BaseModel):
    """学期表彰"""
    SEMESTER_CHOICES = [
        ('first', '第一学期'),
        ('second', '第二学期'),
    ]

    student = models.ForeignKey(Student, on_delete=models.CASCADE, verbose_name='学生')
    academic_year = models.CharField('学年', max_length=20)
    semester = models.CharField('学期', max_length=10, choices=SEMESTER_CHOICES)
    class_rank = models.IntegerField('班级排名', validators=[MinValueValidator(1)], blank=True, null=True)
    grade_point_average = models.FloatField('平均绩点', validators=[MinValueValidator(0.0), MaxValueValidator(5.0)], blank=True, null=True)
    honors_received = models.ManyToManyField(HonorType, verbose_name='获得表彰', blank=True)

    def __str__(self):
        return f"{self.student.name} - {self.academic_year} {self.get_semester_display()}"

    class Meta:
        verbose_name = '学期表彰'
        verbose_name_plural = '学期表彰管理'
        unique_together = ('student', 'academic_year', 'semester')